﻿using System;
using System.Text.RegularExpressions;

namespace LdtJudge.DataEngine.Common.DateAssemble
{
    public class ConvertStingToDateTime
    {
        /// <summary>
        /// Converts to date.
        /// </summary>
        /// <param name="convertValue">The convert value.</param>
        /// <returns>System.String.</returns>
        public static string ConvertToDate(string convertValue)
        {
            try
            {

                DateTime parseDate;
                bool r = DateTime.TryParse(convertValue, out parseDate);
                if (Regex.IsMatch(convertValue, @"[〇零一二三四五六七八九十\s]+"))
                {
                    string years = Regex.Match(convertValue, "[^年]+(?=年)").Value;
                    string months = Regex.Match(convertValue, "[^年月]+(?=月)").Value;
                    string day = Regex.Match(convertValue, "[^年月日]+(?=日)").Value;

                    years = DateTimeFormat.FormatChDateTimeNumber(years).ToString();
                    months = DateTimeFormat.FormatChDateTimeNumber(months).ToString();
                    day = DateTimeFormat.FormatChDateTimeNumber(day).ToString();
                    day = (day == "0") ? "01" : day;

                    convertValue = years + "年" + months + "月" + day + "日";
                }
                else
                {
                    if (!r)
                    {
                        Regex reg = new Regex("[0-9.]+");
                        string result = reg.Match(convertValue).Value;
                        //2018-01-02 添加日期转换，验证，如果是不是日期格式，则进行转换
                        if (!string.IsNullOrEmpty(result))
                            parseDate = DateTime.Parse(result);

                    }
                    else
                    {
                        parseDate = Convert.ToDateTime(convertValue);
                    }
                }
                

                int year = parseDate.Year;
                int month = parseDate.Month;
                int date = parseDate.Day;

                Regex regx = new Regex(@"\d");
                //if (convertValue.Substring(0,2)!="20")
                //{
                //    convertValue = "20" + convertValue;
                //}
                MatchCollection mact = regx.Matches(convertValue);
                string datlet = string.Empty;
                foreach (Match item in mact)
                {
                    datlet += item.Value;
                }
                string NewDateTime = string.Empty;
                string datestr = datlet;
                if (!string.IsNullOrEmpty(datestr) && year == Convert.ToInt32(datestr.Substring(0, year.ToString().Length)))
                {
                    datestr = datestr.Substring(year.ToString().Length);
                    NewDateTime += year + "年";
                    convertValue = NewDateTime;
                }
                datestr = datestr.TrimStart('0');
                if (!string.IsNullOrEmpty(datestr) && month == Convert.ToInt32(datestr.Substring(0, month.ToString().Length)))
                {
                    datestr = datestr.Substring(month.ToString().Length);
                    NewDateTime += month.ToString().PadLeft(2, '0') + "月";
                    convertValue = NewDateTime;
                }
                datestr = datestr.TrimStart('0');
                if (!string.IsNullOrEmpty(datestr) && date == Convert.ToInt32(datestr.Substring(0, date.ToString().Length)))
                {
                    datestr = datestr.Substring(date.ToString().Length);
                    NewDateTime += date.ToString().PadLeft(2, '0') + "日";
                    convertValue = NewDateTime;
                }

            }
            catch (Exception e)
            {
               
            }

            return convertValue;
        }

        /// <summary>
        /// 转数据库日期
        /// YYYY-MM-DD
        /// </summary>
        /// <param name="convertValue">The convert value.</param>
        /// <returns>System.String.</returns>
        public static string ConvertToStandardDate(string convertValue)
        {
            DateTime parseDate;
            if (Regex.IsMatch(convertValue, @"[〇零一二三四五六七八九十\s]+"))
            {
                string years = Regex.Match(convertValue, "[^年]+(?=年)").Value;
                string months = Regex.Match(convertValue, "[^年月]+(?=月)").Value;
                string day = Regex.Match(convertValue, "[^年月日]+(?=日)").Value;

                years = DateTimeFormat.FormatChDateTimeNumber(years).ToString();
                months = DateTimeFormat.FormatChDateTimeNumber(months).ToString();
                day = DateTimeFormat.FormatChDateTimeNumber(day).ToString();
                day = (day == "0") ? "01" : day;

                convertValue = years + "-" + months + "-" + day;
                bool r = DateTime.TryParse(convertValue, out parseDate);
                if (r)
                {
                    //convertValue = parseDate.ToString("yyyy-MM-dd");
                    convertValue = parseDate.ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    convertValue = "";
                }
            }
            else
            {
                bool r = DateTime.TryParse(convertValue, out parseDate);
                if (r)
                {
                    //convertValue = parseDate.ToString("yyyy-MM-dd");
                    convertValue = parseDate.ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    convertValue = "";
                }
            }
            

            return convertValue;
        }

        /// <summary>
        /// 转数据库短日期
        /// YYYY-MM-DD
        /// </summary>
        /// <param name="convertValue">The convert value.</param>
        /// <returns>System.String.</returns>
        public static string ConvertToShortDate(string convertValue)
        {
            DateTime parseDate;
            if (Regex.IsMatch(convertValue, @"[〇零一二三四五六七八九十\s]+"))
            {
                string years = Regex.Match(convertValue, "[^年]+(?=年)").Value;
                string months = Regex.Match(convertValue, "[^年月]+(?=月)").Value;
                string day = Regex.Match(convertValue, "[^年月日]+(?=日)").Value;

                years = DateTimeFormat.FormatChDateTimeNumber(years).ToString();
                months = DateTimeFormat.FormatChDateTimeNumber(months).ToString();
                day = DateTimeFormat.FormatChDateTimeNumber(day).ToString();
                day = (day == "0") ? "01" : day;

                convertValue = years + "-" + months + "-" + day;
                bool r = DateTime.TryParse(convertValue, out parseDate);
                if (r)
                {
                    convertValue = parseDate.ToString("yyyy-MM-dd");
                }
                else
                {
                    convertValue = "";
                }
            }
            else
            {
                bool r = DateTime.TryParse(convertValue, out parseDate);
                if (r)
                {
                    convertValue = parseDate.ToString("yyyy-MM-dd");
                }
                else
                {
                    convertValue = "";
                }
            }


            return convertValue;
        }


    }
}
